package com.xh.fma.commodity.service.impl;

import com.xh.fma.commodity.dao.SpuInfoDescDao;
import com.xh.fma.commodity.entity.SpuInfoDescEntity;
import com.xh.fma.commodity.service.ProductAttrValueService;
import com.xh.fma.commodity.service.SkuInfoService;
import com.xh.fma.commodity.service.SpuImagesService;
import com.xh.fma.commodity.vo.SpuSaveVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.common.utils.PageUtils;
import com.xh.common.utils.Query;

import com.xh.fma.commodity.dao.SpuInfoDao;
import com.xh.fma.commodity.entity.SpuInfoEntity;
import com.xh.fma.commodity.service.SpuInfoService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Resource
    private SpuInfoDescDao spuInfoDescDao;

    @Resource
    private SpuImagesService spuImagesService;

    @Resource
    private ProductAttrValueService productAttrValueService;

    @Resource
    private SkuInfoService skuInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<SpuInfoEntity> wrapper = new QueryWrapper<>();

        // 搜索品牌id
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId)) {
            wrapper.eq("brand_id", brandId);
        }

        // 搜索商品分类
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId)) {
            wrapper.eq("catalog_id", catelogId);
        }

        // 搜商品状态
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("publish_status", status);
        }
        // 搜关键字
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(w -> {
                w.eq("id", key).or().like("spu_name", key);
            });
        }

        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSpuInfo(SpuSaveVO spuSaveVO) {
        // 保存spuInfo信息
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuSaveVO,spuInfoEntity);
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        this.save(spuInfoEntity);


        // 保存desc信息
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());

        List<String> decript = spuSaveVO.getDecript();
        if (decript.size() == 0){
            spuInfoDescEntity.setDecript("default.jpg");
        } else {
            spuInfoDescEntity.setDecript(String.join(",", decript));
        }

        spuInfoDescDao.insert(spuInfoDescEntity);

        // 保存图片信息
        spuImagesService.saveImages(spuInfoEntity.getId(),spuSaveVO.getImages());

        // 保存spu的基本属性信息
        productAttrValueService.saveAttrByVo(spuInfoEntity.getId(), spuSaveVO);

        // 保存sku的信息
        skuInfoService.saveSkuInfo(spuInfoEntity,spuSaveVO);

        // 上架和下架 的方法





    }

    @Override
    public void statusUpdate(Long spuId, Long status) {
        this.baseMapper.statusUpdate(spuId,status);
    }


}